<?php
include './vendor/autoload.php';
include './Mysql.class.php';
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
class abc{
    private $Cilent;

    public function __construct(){
        //通信协议 tcp upd txt  http  websocket
        $con=new Worker("websocket://0.0.0.0:9000");
        $con->name="通信服务器";
        $con->onConnect=[$this,"Connect"];
        $con->onMessage=[$this,"Message"];
        $con::runAll();
        //worker 四个回调函数  onConnect onMessage onError  onClose
    }

    /**
     * 当服务器被客户端链接额时候
     * @param 链接者的信息  Tcp 对象{ 包含了连接着的所有信息}
     * */
    public function Connect(TcpConnection $connection){
        $this->Cilent[]=$connection;
    }
    /**
     * 当服务器接受到客户端发送的消息的 时候
     * 客户端的详细信息 对象 Tcp
     * 客户端发送的内容 变量
     * */
    public function Message(TcpConnection $connection,$data){
        $args=json_decode($data,true);
        $a=new Mysql();
        $name="'".$args['name']."'";
        $content="'".$args['content']."'";
        $sql="insert into user(name,content) values(".$name.",".$content.")";
        $a->connect->query($sql);

        foreach ($this->Cilent as $v){
            $v->send($data);
        }
    }

}

$a=new abc();